package com.easyctrl.fragment;

import com.easyctrl.iface.BaseFragmentImpl;
import com.easyctrl.ldy.activity.R;
import com.easyctrl.ldy.activity.SettingActivity;
import com.easyctrl.ldy.activity.UserActivity;
import com.easyctrl.ldy.view.BackView;
import com.easyctrl.ui.base.BaseEasyCtrlFragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class BaseFragment extends BaseEasyCtrlFragment implements
		OnClickListener {

	public SettingActivity settingActivity;
	public UserActivity userActivity;
 
	private Button operator_one;
	private Button operator_two;
	private Button operator_three;
	private Button operator_four;
	public ListView listview;
	private Button btnFloor;
	private Button btnRoom;
	private Button btnDevice;
	private Button menuButton;
	private BackView backView;

	private String title;
	private View barView;

	public void setTitle(String title) {
		this.title = title;
	}
	
	public void setViewTile(String title){
		if(titleView != null){
			titleView.setText(title);
		}
	}
	
	public void setEnabledOne(boolean isEnable){
		if(operator_one != null){
			operator_one.setEnabled(isEnable);
		}
	}
	
	public void setEnabledTwo(boolean isEnable){
		if(operator_two != null){
			operator_two.setEnabled(isEnable);
		}
	}

	private BaseFragmentImpl fragmentListener;

	public void setFragmentListener(BaseFragmentImpl fragmentListener) {
		this.fragmentListener = fragmentListener;
	}

	public void setButtonFloorString(String str) {
		if (btnFloor != null) {
			btnFloor.setText(str);
		}
	}

	public void setButtonRoomString(String str) {
		if (btnRoom != null) {
			btnRoom.setText(str);
		}
	}

	public void setButtonDeviceString(String str) {
		if (btnDevice != null) {
			btnDevice.setText(str);
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		onCreateInit();
		
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.base_fragment_layout, null);

		findViewByID(view);
		initWidget(view);
		return view;
	}

	private boolean dimissBar;

	public void setDimissBar(boolean dimissBar) {
		this.dimissBar = dimissBar;
	}

	@Override
	public void onClick(View v) {
		if (R.id.operator_one == v.getId()) {
			fragmentListener.onClickOperatorOne(v);
		} else if (R.id.operator_two == v.getId()) {
			fragmentListener.onClickOpreatorTwo(v);
		} else if (R.id.operator_three == v.getId()) {
			fragmentListener.onClickOpreatorThree(v);
		} else if (R.id.operator_four == v.getId()) {
			fragmentListener.onClickOperatorFour(v);
		} else if (R.id.floor == v.getId()) {
			fragmentListener.onClickFloor(v);
		} else if (R.id.room == v.getId()) {
			fragmentListener.onClickRoom(v);
		} else if (R.id.module == v.getId()) {
			fragmentListener.onClickDevice(v);
		} else if (R.id.leftButton == v.getId()) {
			SettingActivity activity = (SettingActivity) getActivity();
			activity.showLeft();
		}
	}

	@Override
	protected void findViewByID(View view) {
		menuButton = (Button) view.findViewById(R.id.leftButton);
		menuButton.setOnClickListener(this);

		operator_one = (Button) view.findViewById(R.id.operator_one);
		operator_two = (Button) view.findViewById(R.id.operator_two);
		operator_three = (Button) view.findViewById(R.id.operator_three);
		operator_four = (Button) view.findViewById(R.id.operator_four);

		operator_one.setText(fragmentListener.onClickOneString());
		operator_three.setText(fragmentListener.onClickThreeString());
		operator_four.setText(fragmentListener.onClickFourString());
		operator_two.setText(fragmentListener.onClickTwoString());
		listview = (ListView) view.findViewById(R.id.listview);
		btnFloor = (Button) view.findViewById(R.id.floor);
		btnRoom = (Button) view.findViewById(R.id.room);
		btnDevice = (Button) view.findViewById(R.id.module);
		barView = view.findViewById(R.id.arealayout);
		backView = (BackView) view.findViewById(R.id.backview);
		titleView = (TextView) backView.findViewById(R.id.title);
		titleView.setText(fragmentListener.onTitle());
	}

	private TextView titleView;
	
	@Override
	protected void initWidget(View view) {
		btnFloor.setOnClickListener(this);
		btnRoom.setOnClickListener(this);
		btnDevice.setOnClickListener(this);

		operator_one.setOnClickListener(this);
		operator_two.setOnClickListener(this);
		operator_three.setOnClickListener(this);
		operator_four.setOnClickListener(this);
 
		listview.setAdapter(fragmentListener.onAdapter());
 
		if (dimissBar) {
			barView.setVisibility(View.GONE);
		} else {
			barView.setVisibility(View.VISIBLE);
		}

	}

	@Override
	protected void onCreateInit() {
		if (getActivity() instanceof SettingActivity) {
			settingActivity = (SettingActivity) this.getActivity();
		} else if (getActivity() instanceof UserActivity) {
			userActivity = (UserActivity) this.getActivity();
		}
	}
}
